import java.util.Arrays;

public class WhiteSpaceEditing {
	public int getMinimum(int[] lines) {
		int lineCount = lines.length;
		int[] lengths = new int[lineCount + 1];
		System.arraycopy(lines, 0, lengths, 1, lineCount);
		lengths[0] = 0;
		Arrays.sort(lengths);
		int[][][] result = new int[lineCount][lineCount][lineCount + 1];
		for (int[][] result0 : result) {
			for (int[] result1 : result0)
				Arrays.fill(result1, -1);
		}
		int[][][] fixedResult = new int[lineCount][lineCount][lineCount + 1];
		for (int[][] result0 : fixedResult) {
			for (int[] result1 : result0)
				Arrays.fill(result1, -1);
		}
		return countResult(0, lineCount - 1, 0, result, fixedResult, lines, lengths);
	}

	private int countResult(int from, int to, int value, int[][][] result, int[][][] fixedResult, int[] lines, int[] lengths) {
		if (result[from][to][value] != -1)
			return result[from][to][value];
		if (from == to)
			return result[from][to][value] = Math.abs(lines[from] - lengths[value]);
		result[from][to][value] = Integer.MAX_VALUE;
		for (int i = 0; i < lengths.length; i++)
			result[from][to][value] = Math.min(result[from][to][value], countFixedResult(from, to, i, result, fixedResult, lines, lengths) + Math.abs(lengths[i] - lengths[value]));
		return result[from][to][value];
	}

	private int countFixedResult(int from, int to, int value, int[][][] result, int[][][] fixedResult, int[] lines, int[] lengths) {
		if (fixedResult[from][to][value] != -1)
			return fixedResult[from][to][value];
		fixedResult[from][to][value] = Integer.MAX_VALUE;
		for (int i = from; i < to; i++)
			fixedResult[from][to][value] = Math.min(fixedResult[from][to][value], 1 + countResult(from, i, value, result, fixedResult, lines, lengths) + countResult(i + 1, to, value, result, fixedResult, lines, lengths));
		return fixedResult[from][to][value];
	}


	// BEGIN CUT HERE
	public static void main(String[] args) {
		if (args.length == 0) {
			WhiteSpaceEditingHarness.run_test(-1);
		} else {
			for (int i=0; i<args.length; ++i)
				WhiteSpaceEditingHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class WhiteSpaceEditingHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(int expected, int result) { return expected == result; }
	static String formatResult(int res) {
		return String.format("%d", res);
	}
	
	static int verifyCase(int casenum, int expected, int received) { 
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch(casenum) {
		case 0: {
			int[] lines               = { 3, 2, 3 };
			int expected__            = 6;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}
		case 1: {
			int[] lines               = { 0 };
			int expected__            = 0;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}
		case 2: {
			int[] lines               = { 1, 2, 4 } ;
			int expected__            = 6;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}
		case 3: {
			int[] lines               = { 250, 105, 155, 205, 350 } ;
			int expected__            = 499;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}

		// custom cases

/*      case 4: {
			int[] lines               = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}*/
/*      case 5: {
			int[] lines               = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}*/
/*      case 6: {
			int[] lines               = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new WhiteSpaceEditing().getMinimum(lines));
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
